package top.minuy.structure.tree.segment;

import top.minuy.util.operation.Merger;

import java.util.Arrays;

/**
 * 线段树
 *
 * @author Minuy
 * @time 15:41
 * @date 2021/10/4
 * @param <E>
 */
public class SegmentTree<E> {
    private E[] data;
    private E[] tree;
    Merger<E> merger;

    public SegmentTree(E[] arr, Merger<E> merger) {
        this.merger = merger;

        data = (E[]) new Object[arr.length];
        tree = (E[]) new Object[arr.length * 4];

        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }

        buildSegmentTree(0, 0, arr.length - 1);
    }

    private void buildSegmentTree(int treeIndex, int l, int r) {
        // 要构建的线段树只有一个元素了
        if (l == r) {
            // 直接赋值即可
            tree[treeIndex] = data[l];
            return;
        }

        // 计算分割位置和计算左右节点位置
        int mid = l + (r - l) / 2;
        int leftChildIndex = getLeftChild(treeIndex);
        int rightChildIndex = getRightChild(treeIndex);

        // 构建左子树
        buildSegmentTree(leftChildIndex, l, mid);
        // 构建右子树
        buildSegmentTree(rightChildIndex, mid + 1, r);

        // 合并两个子节点
        tree[treeIndex] = merger.merge(tree[leftChildIndex], tree[rightChildIndex]);
    }

    public int getSize() {
        return data.length;
    }

    public void set(int index, E e) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        }
        set(0, 0, data.length - 1, index, e);
    }

    public void set(int treeIndex, int l, int r, int index, E e) {
        if (r == l/*index == l &&  这个在调用的时候就可以保证，mid or mid+1*/) {
            tree[treeIndex] = e;
            return;
        }

        int mid = l + (r - l) / 2;
        int leftChildIndex = getLeftChild(treeIndex);
        int rightChildIndex = getRightChild(treeIndex);

        if (index > mid) { // 在右边，[mid+1,r]
            set(rightChildIndex, mid + 1, r, index, e);
        } else { // 在左边，[l,mid]
            set(leftChildIndex, l, mid, index, e);
        }
        // 值可能被改变
        tree[treeIndex] = merger.merge(tree[leftChildIndex], tree[rightChildIndex]);
    }

    public E get(int index) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        }
        return data[index];
    }

    private int getLeftChild(int index) {
        return index * 2 + 1;
    }

    private int getRightChild(int index) {
        return index * 2 + 2;
    }

    public E query(int queryL, int queryR) {
        if (queryL < 0 || queryR < 0 || queryL >= data.length || queryR >= data.length || queryL > queryR) {
            throw new IllegalArgumentException("Query failed. Index is illegal.");
        }
        return query(0, 0, data.length - 1, queryL, queryR);
    }

    private E query(int treeIndex, int l, int r, int queryL, int queryR) {
        // 如果完全吻合要查询的区间就可以直接返回了
        if (l == queryL && r == queryR) {
            return tree[treeIndex];
        }
        // 获取中间的索引
        int mid = l + (r - l) / 2;
        int leftChildIndex = getLeftChild(treeIndex);
        int rightChildIndex = getRightChild(treeIndex);

        if (queryR <= mid) { // 查询的区间完全在左半部分
            return query(leftChildIndex, l, mid, queryL, queryR);
        } else if (queryL > mid) { // 查询的区间完全在右半部分
            return query(rightChildIndex, mid + 1, r, queryL, queryR);
        } else { // 查询的部分左右都有
            E leftE = query(leftChildIndex, l, mid, queryL, mid);
            E rightE = query(rightChildIndex, mid + 1, r, mid + 1, queryR);
            return merger.merge(leftE, rightE);
        }
    }

    @Override
    public String toString() {
        return Arrays.toString(tree);
    }
}
